package com.sinosoft.sm.message.SDUtils;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import smscommon.SmsClient;
import smsmessage.sms.SMSclientSubmit;


public final class SDUtils {

	private static Logger logger = Logger.getLogger(SDUtils.class);
	public static void main(String args[]) throws Exception {
		SmsClient smsClient=SmsClient.getInstance();//建立连接
		SMSclientSubmit mt = new SMSclientSubmit();///实例化用于下发短信的对象
		String massTel = "18303096944,18772382251";//群发以逗号分割，最多100个
		mt.setDestID(massTel);//接收号码
		mt.setPriority(1);// 设置优先级，默认为1
		mt.setMsgContent("双十一快乐");// 短信内容
		List<String> sendIDList = new ArrayList<String>();
		sendIDList = mt.sendSMSList();
		sendIDList.add("1");
		try {
			while ("1".equals(sendIDList.get(0))
					|| "2".equals(sendIDList.get(0))
					|| "3".equals(sendIDList.get(0))
					|| "4".equals(sendIDList.get(0))
					|| "5".equals(sendIDList.get(0))
					|| "6".equals(sendIDList.get(0))) {
				Thread.sleep(1000);
				if ("1".equals(sendIDList.get(0))) {
					logger.error("信息下发缓冲队列已满");
				} else if ("2".equals(sendIDList.get(0))) {
					logger.error("没有建立连接");
				} else if ("3".equals(sendIDList.get(0))) {
					logger.error("流量超出设定值");
				} else if ("4".equals(sendIDList.get(0))) {
					logger.error("短信长度超出限制");
					break;
				} else if ("5".equals(sendIDList.get(0))) {
					logger.error("群发数量超出");
				} else if ("6".equals(sendIDList.get(0))) {
					logger.error("分隔符不正确");
				} else {
					String[] arg = massTel.split(",");
					for (int i = 1; i < sendIDList.size(); i++) {
						logger.info("企业下行消息标识 [" + sendIDList.get(i) + "]，"
								+ "目的号码 ["
								+ arg[i - 1] + "]，" + "短信内容 ["
								+ mt.getMsgContent() + "]");
					}
				}
			}
		} catch (Exception e) {
			logger.error("你又错了");
		}
	} 

	public static String sendMessage(String phone,String message){
		SmsClient.getInstance();//建立连接
		SMSclientSubmit mt = new SMSclientSubmit();///实例化用于下发短信的对象
		mt.setDestID(phone);//接收号码
		mt.setPriority(1);// 设置优先级，默认为1
		mt.setMsgContent(message);// 短信内容
		List<String> sendIDList = new ArrayList<String>();
		sendIDList = mt.sendSMSList();
		String errorMessage = null;
		for(String sendList:sendIDList){
			switch(sendList){
			case"1":
				logger.error("信息下发缓冲队列已满");
				errorMessage = "信息下发缓冲队列已满";
				break;
			case"2":
				logger.error("没有建立连接");
				errorMessage = "没有建立连接";
				break;
			case"3":
				logger.error("流量超出设定值");
				errorMessage = "流量超出设定值";
				break;
			case"4":
				logger.error("短信长度超出限制");
				errorMessage = "短信长度超出限制";
				break;
			case"5":
				logger.error("群发数量超出");
				errorMessage = "群发数量超出";
				break;
			case"6":
				logger.error("分隔符不正确");
				errorMessage = "分隔符不正确";
				break;
			}
		}
		return errorMessage;
	}
	
}
